<template>
  <el-form class="query-container" ref="queryFormRef" :model="queryParams" :inline="true" @submit.native.prevent>
    <el-form-item>
      <el-button @click="$router.back()"><el-icon><Back /></el-icon> 返回</el-button>
    </el-form-item>
  </el-form>

  <div class="inner-container">
    <el-row :gutter="10" style="margin-bottom:8px">
      <el-col :span="1.5">
        <el-button type="primary" plain @click="showAddDialog">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain @click="showEditDialog" :disabled="selectedIds.length!==1">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain @click="handleDelete(null)" :disabled="!selectedIds.length">删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="tableData" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50"/>
      <el-table-column prop="dictId" label="字典编号"/>
      <el-table-column prop="label" show-overflow-tooltip label="字典项名"/>
      <el-table-column prop="value" label="字典项值"/>
      <el-table-column prop="sort" label="排序序号"/>
      <el-table-column prop="remark" label="备注信息" show-overflow-tooltip/>
      <el-table-column prop="createTime" label="创建时间" show-overflow-tooltip/>
      <el-table-column label="操作" fixed="right" width="150">
        <template #default="scope">
          <el-button type="primary" link icon="Edit" @click="showEditDialog(scope.row)">修改</el-button>
          <el-button type="danger" link icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <br>
    <el-pagination
        :total="total"
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />

    <el-dialog v-model="dialogVisible" :title="dialogTitle" :before-close="closeDialog" append-to-body>
      <el-form ref="dialogFormRef" :model="formData" :rules="formRules" label-width="80px">
        <el-form-item label="字典编号">
          <el-input v-model="formData.dictId" disabled/>
        </el-form-item>
        <el-form-item label="字典项名" prop="label">
          <el-input v-model="formData.label" placeholder="请输入字典项名"/>
        </el-form-item>
        <el-form-item label="字典项值" prop="value">
          <el-input v-model="formData.value" placeholder="请输入字典项值"/>
        </el-form-item>
        <el-form-item label="排序序号" prop="sort">
          <el-input v-model="formData.sort" placeholder="请输入排序序号"/>
        </el-form-item>
        <el-form-item label="备注信息" prop="remark">
          <el-input v-model="formData.remark" placeholder="请输入备注信息" type="textarea" :rows="3"/>
        </el-form-item>
      </el-form>
      <template #footer class="dialog-footer">
        <el-button type="primary" @click="saveOrUpdate">确定</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {nextTick, onMounted, ref} from 'vue'
import {ElMessage, ElMessageBox} from 'element-plus'
import request from '@/utils/request'
import {useRoute} from "vue-router"

// --------------------------------------------------
const loading = ref(false)
const queryFormRef = ref()
const queryParams = ref({pageNum: 1, pageSize: 10, dictId: undefined})
const total = ref(0)
const tableData = ref([])
const selectedIds = ref([])

const listData = () => {
  loading.value = true
  request.get('/api/sys/dict/options/select', {
    params: queryParams.value
  }).then(res => {
    total.value = res.data.total
    tableData.value = res.data.list
    loading.value = false
  })
}

const handleSelectionChange = (selection) => {
  selectedIds.value = selection.map(item => item.id)
}

const handleQuery = () => {
  queryParams.value.pageNum = 1
  listData()
}

const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const handleCurrentChange = (val) => {
  queryParams.value.pageNum = val
  listData()
}

const handleSizeChange = (val) => {
  queryParams.value.pageSize = val
  listData()
}

const handleDelete = (row) => {
  let ids = row ? [row.id] : [...selectedIds.value]
  if (ids.length === 0) {
    ElMessage.error('请选择要删除的记录')
    return
  }
  ElMessageBox.confirm('确定删除吗？', '提示', {
    type: 'warning'
  }).then(() => {
    request.delete(`/api/sys/dict/options/delete/`+ids)
        .then(res => {
          selectedIds.value.length = 0
          listData()
          ElMessage.success('删除成功')
        })
  }).catch(() => {})
}

// --------------------------------------------------
const dialogTitle = ref('')
const dialogVisible = ref(false)
const dialogFormRef = ref()
const formData = ref({
  id: undefined,
  dictId: undefined,
  label: undefined,
  value: undefined,
  sort: undefined,
  remark: undefined
})
const formRules = ref({
  label: [{required: true, message: '字典项名为必填项'}],
  value: [{required: true, message: '字典项值为必填项'}],
  sort: [{required: true, message: '排序序号为必填项'}],
  remark: [{required: true, message: '描述信息为必填项'}]
})

const showAddDialog = () => {
  dialogTitle.value = '新增字典项'
  dialogVisible.value = true
}

const showEditDialog = (row) => {
  let id = row.id || selectedIds.value
  request.get(`/api/sys/dict/options/select?id=`+id)
      .then(res => {
        formData.value = res.data.list[0]
        dialogTitle.value = '修改字典项'
        dialogVisible.value = true
      })
}

const closeDialog = () => {
  formData.value = {
    id: undefined,
    dictId: route.query.dictId,
    label: undefined,
    value: undefined,
    sort: undefined,
    remark: undefined
  }
  dialogFormRef.value.resetFields()
  dialogVisible.value = false
}

const saveOrUpdate = () => {
  dialogFormRef.value.validate(valid => {
    if (!valid) {
      ElMessage.error('请正确填写参数')
      return
    }
    const id = formData.value.id
    /*request.post('/api/sys/dict/options/save',formData.value).then(res=>{
      ElMessage.success(id ? '修改成功' : '新增成功')
      formData.value = res.data
      console.log(formData.value)
      closeDialog()
      listData()
    })*/
    request({
      url: '/api/sys/dict/options/save',
      method: 'post',
      data: formData.value
    }).then(res => {
      console.log(formData.value)
      closeDialog()
      listData()
      ElMessage.success(id ? '修改成功' : '新增成功')
    })
  })
}

// --------------------------------------------------
const route = useRoute()
onMounted(() => {
  queryParams.value.dictId = route.query.dictId
  formData.value.dictId = route.query.dictId
  listData()
})
</script>

<style scoped>

</style>
